[Create a Namespace]
$ kubectl create namespace default-mem-example

[Create the LimitRange in the default-mem-example Namespace]
$ kubectl apply -f LimitRange_1.yaml --namespace=default-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults.yaml --namespace=default-mem-example

Now if you create a Pod in the default-mem-example namespace, and any container within that Pod does not specify its own values for memory request and memory limit. 
Then the control plane applies default values: a memory request of 256MiB and a memory limit of 512MiB.

[Create the Pod]
$ kubectl apply -f Pod_1.yaml --namespace=default-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod.yaml --namespace=default-mem-example

[View detailed information about the Pod]
$ kubectl get pod default-mem-demo --output=yaml --namespace=default-mem-example
...
containers:
- image: nginx
  imagePullPolicy: Always
  name: default-mem-demo-ctr
  resources:
    limits:
      memory: 512Mi
    requests:
      memory: 256Mi

[Delete the Pod]
$ kubectl delete pod default-mem-demo --namespace=default-mem-example


⦿ What if you specify a container's limit, but not its request ?

[Create the Pod]
$ kubectl apply -f Pod_2.yaml --namespace=default-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod-2.yaml --namespace=default-mem-example

[View detailed information about the Pod]
$ kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example
...
resources:
  limits:
    memory: 1Gi
  requests:
    memory: 1Gi

The output shows that the container's memory request is set to match its memory limit. 
Notice that the container was not assigned the default memory request value of 256Mi.


⦿ What if you specify a container's request, but not its limit ?

[Create the Pod]
$ kubectl apply -f Pod_3.yaml --namespace=default-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod-3.yaml --namespace=default-mem-example

[View the Pod's specification]
$ kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example
...
resources:
  limits:
    memory: 512Mi
  requests:
    memory: 128Mi

The container is limited to use no more than 512MiB of memory, which matches the default memory limit for the namespace.


⦿ Motivation for default memory limits and requests
If a Namespace has a memory resource quota configured, it is helpful to have a default value in place for memory limit. 
Here are three of the restrictions that a resource quota imposes on a namespace:
For every Pod that runs in the namespace, the Pod and each of its containers must have a memory limit. (If you specify a memory limit for every container in a Pod, Kubernetes can infer the Pod-level memory limit by adding up the limits for its containers).
Memory limits apply a resource reservation on the node where the Pod in question is scheduled. The total amount of memory reserved for all Pods in the namespace must not exceed a specified limit.
The total amount of memory actually used by all Pods in the namespace must also not exceed a specified limit.
When add a LimitRange:
If any Pod in that namespace that includes a container does not specify its own memory limit. 
The control plane applies the default memory limit to that container. 
The Pod can be allowed to run in a namespace that is restricted by a memory ResourceQuota.

[Delete the Namespace]
$ kubectl delete namespace default-mem-example
